﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp
{
    public partial class Form17 : Form
    {
        public Form17()
        {
            InitializeComponent();
        }

        private int current = 1;
        public string conStr = "Data Source=43.139.213.82;Initial Catalog=school;Persist Security Info=True;User ID=sa;Password=fgb@1357426";
        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void ShowCurrentStudent() {
            string sql = "select * from student where Num=" + current;
            SqlConnection sqlConnection=new SqlConnection(conStr);
            sqlConnection.Open();
            //创建command对象
            SqlCommand sqlCommand=new SqlCommand(sql,sqlConnection);
            SqlDataReader sqlDataReader=sqlCommand.ExecuteReader();
            if (sqlDataReader.Read())
            {
                textBox1.Text=sqlDataReader.GetInt32(0).ToString();
                textBox2.Text=sqlDataReader.GetString(1);
                string  sex=sqlDataReader.GetString(i: 3);
                if (sex == "男")
                {
                    radioButton1.Checked = true;
                } 
                else { 
                 radioButton2.Checked=true;
                }
            }
        }

        /// <summary>
        /// 加载当前学生信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form17_Load(object sender, EventArgs e)
        {
              ShowCurrentStudent();
        }

        /// <summary>
        /// 上一个
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            current--;
            ShowCurrentStudent();
        }

        /// <summary>
        /// 下一个
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            current++;
            ShowCurrentStudent();
        }

        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            string sex = "男";
            if (radioButton2.Checked) {
                sex = "女";
            }
            SqlConnection sqlConnection=new SqlConnection(conStr);
            SqlCommand sqlCommand = new SqlCommand("insert into student(Num,Name,Sex) values(@Num,@Name,@Sex)", sqlConnection);
            sqlCommand.Parameters.AddWithValue("@Num", 4);
            sqlCommand.Parameters.AddWithValue("@Name", textBox2.Text);
            sqlCommand.Parameters.AddWithValue("@Sex", sex);
            sqlConnection.Open();
            sqlCommand.ExecuteNonQuery();
            sqlConnection.Close();
            MessageBox.Show("成功添加一条数据! ");
        }

        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click(object sender, EventArgs e)
        {
            string sex = "男";
            if (radioButton2.Checked)
            {
                sex = "女";
            }
            SqlConnection sqlConnection = new SqlConnection(conStr);
            SqlCommand sqlCommand = new SqlCommand("update student set Name=@Num,Sex=@Sex where Num=@Num", sqlConnection);
            sqlCommand.Parameters.AddWithValue("@Name", textBox2.Text);
            sqlCommand.Parameters.AddWithValue("@Sex", sex);
            sqlCommand.Parameters.AddWithValue("@Num", Convert.ToInt32(textBox1.Text));
            sqlConnection.Open();
            sqlCommand.ExecuteNonQuery();
            sqlConnection.Close();
            MessageBox.Show("成功修改一条数据");
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button6_Click(object sender, EventArgs e)
        {
            SqlConnection sqlConnection=new SqlConnection(conStr);
            SqlCommand sqlCommand = new SqlCommand("delete from student where Num=@Num",sqlConnection);
            sqlCommand.Parameters.AddWithValue("@Num", Convert.ToInt32(textBox1.Text));
            sqlConnection.Open(); 
            sqlCommand.ExecuteNonQuery(); 
            sqlConnection.Close();
            current++;
            ShowCurrentStudent();
            MessageBox.Show("成功删除一条数据");
        }
    }
}
